package com.changyinBus.web.controller.system;

import com.changyinBus.common.annotation.Log;
import com.changyinBus.common.core.controller.BaseController;
import com.changyinBus.common.core.domain.AjaxResult;
import com.changyinBus.common.core.domain.entity.SysSite;
import com.changyinBus.common.core.page.TableDataInfo;
import com.changyinBus.common.enums.BusinessType;
import com.changyinBus.common.utils.IDGenerator;
import com.changyinBus.system.service.ISysSiteService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/site")
public class SysSiteController extends BaseController {
    @Resource
    private ISysSiteService sysSiteService;

    /**
     * 获取站点列表
     */
    @PreAuthorize("@ss.hasPermi('system:site:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSite site) {
        startPage();
        List<SysSite> list = sysSiteService.selectSiteList(site);
        return getDataTable(list);
    }

    /**
     * 获取站点列表
     */
    @PreAuthorize("@ss.hasPermi('system:site:list')")
    @GetMapping("/allList")
    public AjaxResult allList() {
        return success(sysSiteService.selectSiteList(new SysSite()));
    }

    /**
     * 根据站点编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:site:query')")
    @GetMapping(value = "/{siteId}")
    public AjaxResult getInfo(@PathVariable Long siteId) {
        return success(sysSiteService.selectSiteById(siteId));
    }

    /**
     * 新增站点
     */
    @PreAuthorize("@ss.hasPermi('system:site:add')")
    @Log(title = "站点管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysSite site) {
        site.setSiteId(IDGenerator.getID());
        site.setCreateBy(getUsername());
        return toAjax(sysSiteService.insertSite(site));
    }

    /**
     * 修改站点
     */
    @PreAuthorize("@ss.hasPermi('system:site:edit')")
    @Log(title = "站点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysSite site) {
        site.setUpdateBy(getUsername());
        return toAjax(sysSiteService.updateSite(site));
    }

    /**
     * 删除站点
     */
    @PreAuthorize("@ss.hasPermi('system:site:remove')")
    @Log(title = "站点管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{siteId}")
    public AjaxResult remove(@PathVariable Long siteId) {
        if (siteId == 1){
            return error("默认站点无法删除！");
        }
        return toAjax(sysSiteService.deleteSiteById(siteId));
    }
}
